if not pattern then pattern = {} end
if not pattern.ticks then pattern.ticks = {} end

if not pattern.ticks.pressure then

pattern.ticks.pressure = {}
pattern.ticks.pressure.trends = {}

dofile (ScriptsRootPath .. "lib\\ticks.lua")

function pattern.ticks.pressure.Init()
	pattern.ticks.pressure.params = lib:LoadIni(ticks.patterns_init, "pressure", {level = 10, trend = 0.5, period = 30})
	if not ticks.clean_period
		or ticks.clean_period < pattern.ticks.pressure.params.period * 60
	then
		ticks.clean_period = pattern.ticks.pressure.params.period * 60
	end
	if processor and processor.log then
		processor:log("pattern.ticks.pressure.params", pattern.ticks.pressure.params)
	end
end

function pattern.ticks.pressure.OnTick(tick)
	if not tick then return end
	if not pattern.ticks.pressure.params then pattern.ticks.pressure.Init() end
	
	local tmin = ticks:seconds(tick) - 30
	local d, f, hint, notif
	local level = pattern.ticks.pressure.params.level
	local price, datetime
	local n = ticks:get(tick.sec_code)
			
	if tick.pressure_bid / tick.average_pressure_bid >= level then
		for i = 1, n do
			d = ticks:get(tick.sec_code, i)
			if ticks:seconds(d) < tmin then
				f = true
				break
			end
			if d.pressure_bid / d.average_pressure_bid >= level then
				break
			end
		end
		if f then
			price = tick.price_bid
			
			if not pattern.ticks.pressure.trends[tick.sec_code] then pattern.ticks.pressure.trends[tick.sec_code] = {} end
			ticks:GetTrend(tick, pattern.ticks.pressure.trends[tick.sec_code], pattern.ticks.pressure.params.period)
			
			hint = string.format("PRESS %.0f BID %s %s %s%%",
				tick.pressure_bid / tick.average_pressure_bid,
				lib:FormatPrice(price),
				lib:bool2side(pattern.ticks.pressure.trends[tick.sec_code].is_long),
				tostring(pattern.ticks.pressure.trends[tick.sec_code].percent)
			)
			notif = tick.sec_code .. " " .. hint
			if processor and processor.log then
				processor:log(notif .. " " .. lib:toline(pattern.ticks.pressure.trends[tick.sec_code]))
			end
			
			if pattern.ticks.pressure.trends[tick.sec_code].percent
				and pattern.ticks.pressure.trends[tick.sec_code].percent < pattern.ticks.pressure.params.trend
			then return end
			
			datetime = lib:tickdate(tick.time)
						
			datetime.sec_code__ = tick.sec_code
			datetime.labels__ = {
				lm = tick.price_bid,
				sl = pattern.ticks.pressure.trends[tick.sec_code].pmax or tick.price_bid * 1.0025,
				tp = pattern.ticks.pressure.trends[tick.sec_code].pmin or tick.price_bid * 0.9925
			}
			
			datetime.is_long__ = true
			datetime.hint__ = hint
			datetime.notify__ = notif
	
			return price, datetime
		end
	end
			
	if tick.pressure_offer / tick.average_pressure_offer >= level then
		for i = 1, n do
			d = ticks:get(tick.sec_code, i)
			if ticks:seconds(d) < tmin then
				f = true
				break
			end
			if d.pressure_offer / d.average_pressure_offer >= level then
				break
			end
		end
		if f then
			price = tick.price_offer
			
			if not pattern.ticks.pressure.trends[tick.sec_code] then pattern.ticks.pressure.trends[tick.sec_code] = {} end
			ticks:GetTrend(tick, pattern.ticks.pressure.trends[tick.sec_code], pattern.ticks.pressure.params.period)
			
			hint = string.format("PRESS %.0f OFFER %s %s %s%%",
				tick.pressure_offer / tick.average_pressure_offer,
				lib:FormatPrice(price),
				lib:bool2side(pattern.ticks.pressure.trends[tick.sec_code].is_long),
				tostring(pattern.ticks.pressure.trends[tick.sec_code].percent)
			)
			notif = tick.sec_code .. " " .. hint
			if processor and processor.log then
				processor:log(notif .. " " .. lib:toline(pattern.ticks.pressure.trends[tick.sec_code]))
			end
			
			if pattern.ticks.pressure.trends[tick.sec_code].percent
				and pattern.ticks.pressure.trends[tick.sec_code].percent < pattern.ticks.pressure.params.trend
			then return end
			
			datetime = lib:tickdate(tick.time)
						
			datetime.sec_code__ = tick.sec_code
			datetime.labels__ = {
				lm = tick.price_offer,
				sl = pattern.ticks.pressure.trends[tick.sec_code].pmin or tick.price_bid * 0.9975,
				tp = pattern.ticks.pressure.trends[tick.sec_code].pmax or tick.price_bid * 1.0075
			}
			
			datetime.is_long__ = false
			datetime.hint__ = hint
			datetime.notify__ = notif
	
			return price, datetime
		end
	end
end

end -- of file


